package com.winshare.saleAssistant.service.impl;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.winshare.saleAssistant.dao.OrganizationMapper;
import com.winshare.saleAssistant.domain.dto.req.StaffCreateDTO;
import com.winshare.saleAssistant.domain.dto.req.StaffExcelImportData;
import com.winshare.saleAssistant.domain.entity.OrganizationDO;
import com.winshare.saleAssistant.exception.BusinessException;
import com.winshare.saleAssistant.mapper.StaffMapStruct;
import com.winshare.saleAssistant.service.StaffService;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class ExcelImportDataListener extends AnalysisEventListener<StaffExcelImportData> {

    private final StaffService staffService;

    private final OrganizationMapper organizationMapper;

    private static Map<String, Integer> orgIdMap = new HashMap<>();

    public ExcelImportDataListener(StaffService staffService, OrganizationMapper organizationMapper) {
        this.staffService = staffService;
        this.organizationMapper = organizationMapper;
    }

    @Override
    public void onException(Exception exception, AnalysisContext context) {
        if (exception instanceof ExcelDataConvertException) {
            ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException) exception;
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("第").append(excelDataConvertException.getRowIndex()).append("行，第")
                    .append(excelDataConvertException.getColumnIndex()).append("列解析异常，数据为：")
                    .append(excelDataConvertException.getCellData());
            throw new BusinessException(stringBuilder.toString());
        } else if(exception instanceof BusinessException) {
            throw new BusinessException(exception.getMessage());
        }
    }

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
    }

    @Override
    public void invoke(StaffExcelImportData data, AnalysisContext context) {
        if (orgIdMap.containsKey(data.getOrgCode())) {
            data.setOrgId(orgIdMap.get(data.getOrgCode()));
        } else {
            OrganizationDO organization = organizationMapper.findOrganization(data.getOrgCode(), data.getOrgName());
            if(null == organization) {
                StringBuilder msg = new StringBuilder();
                msg.append("当前机构在系统中不存在,orgCode:").append(data.getOrgCode()).append(",orgName:").append(data.getOrgName());
                throw new BusinessException(msg.toString());
            }
            data.setOrgId(organization.getId());
            StaffCreateDTO staffCreateDTO = StaffMapStruct.INSTANCE.fromStaffExcelImportData(data);
            staffService.save(staffCreateDTO);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
    }

}
